package cs419.magnusmi.trackitcs;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class Delete extends ActionBarActivity {

    String user;
    String API;
    String id;
    ItemDelete killItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = getIntent();
        user = i.getStringExtra("user");
        API = i.getStringExtra("API");
        id = i.getStringExtra("id");

        killItem = new ItemDelete();
        killItem.execute(id);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.delete, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * Represents an asynchronous task to edit item data
     * the user.
     */
    public class ItemDelete extends AsyncTask<String, Void, Boolean> {
        private String response = "";


        @Override
        protected Boolean doInBackground(String... items) {
            //PHP address for editing
            String nuke = "http://web.engr.oregonstate.edu/~cohenad/cs419/index.php/DeleteItem";

            //get item object
            String id = items[0];

            //get item object data
            JSONParser jsonParser = new JSONParser();
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            parameters.add(new BasicNameValuePair("id", id));

            //post changes to database
            JSONObject json = jsonParser.makeHttpRequest(nuke, "POST", parameters);

            Boolean errors;
            try {
                response = json.getString("message");
                errors = json.getBoolean("error");
            } catch (JSONException e) {
                errors = true;
            }

            return errors;
        }

        @Override
        protected void onPostExecute(final Boolean errors) {
            killItem = null;

            boolean success = !errors;  //no errors = success

            //if edit successful, inform user and return to detail view so user can confirm
            if (success) {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, response, duration);
                toast.show();
                Intent i = new Intent(getApplicationContext(), Inventory.class);
                Utility.addToIntent(i, user, API);
                startActivity(i);
                //upon failure, alert user, and return to edit page
            } else {
                Context context = getApplicationContext();
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, response, duration);
                toast.show();
                finish();
            }
        }
    }

}
